const app = getApp()
const db = wx.cloud.database();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // officenotice: [],

    lunbo2: ["https://tse1-mm.cn.bing.net/th/id/OIP-C.IzegLNL499xbVOOH2KKP3wHaEo?w=276&h=180&c=7&r=0&o=5&dpr=1.25&pid=1.7",
     "https://shp.qpic.cn/cfwebcap/0/96a1c8ddbd2fa64fd540aa4d2003f1b0/0/?width=3972&height=1986", 
     "https://tse2-mm.cn.bing.net/th/id/OIP-C.dF-6wtzwObfhYHXl67IpNgHaEE?w=287&h=180&c=7&r=0&o=5&dpr=1.25&pid=1.7",
      "https://tse2-mm.cn.bing.net/th/id/OIP-C.TpczxUenRK3yFaLnJebnoAHaEK?w=328&h=184&c=7&r=0&o=5&dpr=1.25&pid=1.7", 
      "https://tse3-mm.cn.bing.net/th/id/OIP-C.cM7eemKMDVu5KbEmAAn5ZgHaEK?w=307&h=180&c=7&r=0&o=5&dpr=1.25&pid=1.7"],
    fashion: [
      {
        path1:"cloud://caicren1-9ga67iu1c258fa35.6361-caicren1-9ga67iu1c258fa35-1307541336/news/01.jpg",
        id: "GoHome11",
      },
      {
        path1:"cloud://caicren1-9ga67iu1c258fa35.6361-caicren1-9ga67iu1c258fa35-1307541336/news/02.jpg",
        id: "GoHome12",
      },
      {
        path1:"cloud://caicren1-9ga67iu1c258fa35.6361-caicren1-9ga67iu1c258fa35-1307541336/news/03.jpg",
        id: "GoHome13",
      },
    ],
    bg2: false,
    UpDate: [{
        imgPath: "https://game.gtimg.cn/images/gp/zlkdatasys/images/image/20220112/16419806324878.jpg",
        text: "主题玩法"
      },
      {
        imgPath: "cloud://caicren1-9ga67iu1c258fa35.6361-caicren1-9ga67iu1c258fa35-1307541336/news/newGun.jpg",
        text: "枪械升级"
      },
      {
        imgPath: "https://game.gtimg.cn/images/gp/cp/a20211230xc/p1_img1.png",
        text: "全新地图"
      }, {
        // imgPath: "https://game.gtimg.cn/images/gp/cp/a20211230xc/p7_img3.png",
        imgPath: "https://game.gtimg.cn/images/gp/cp/a20211230xc/lock2.png",
        text: "更多特色玩法"
      },
    ],

    Game: [{
        text1: "总决赛 第一天",
        text2: "5-25 18:00",
        text3: "订阅",
        id: "12asd1"
      },
      {
        text1: "总决赛 第二天",
        text2: "5-26 18:00",
        text3: "订阅",
        id: "asdad12312"
      },
      {
        text1: "总决赛 第三天",
        text2: "5-27 18:00",
        text3: "订阅",
        id: "asdsad123"
      },
      {
        text1: "总决赛 第四天",
        text2: "5-28 18:00",
        text3: "订阅",
        id: "asdasd12123"
      },
    ],

    EventNews: [],
  },

  ChangeOffice() {
    this.setData({
      display1: "block",
      display2: "none",
      color1: "#ffffff",
      bgcolor1: "rgb(84, 149, 230)",
      color2: "#8f8f8f",
      bgcolor2: "rgba(212, 212, 212, 0.4)",
      fw1: "800",
      fw2: "300",
    })
  },

  ChangeMatch() {
    this.setData({
      display2: "block",
      display1: "none",
      color2: "#ffffff",
      bgcolor2: "rgb(84, 149, 230)",
      color1: "#8f8f8f",
      bgcolor1: "rgba(212, 212, 212, 0.4)",
      fw2: "800",
      fw1: "300",
    })
  },

  // 测试——web端是否可通过该云函数拿到云数据库eventNews的数据
  getListData(){
    wx.cloud.callFunction({
      name: 'getListYes'
    })
    .then((res)=>{
      console.log("getListData res",res.result.data);
    })
  },

  zhiding() {
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 300
    })
  },

  getUserOpenId(){
    wx.cloud.callFunction({
      name: 'getOpenId',
      complete: res => {
        app.currentUserOpenid = res.result.openid
        // console.log('云函数获取到当前使用小程序的用户的openid: ', app.currentUserOpenid)
      }
    })
  },

  judgeOpenid(){
    db.collection("userInfo").where({
      _openid: app.currentUserOpenid,  // 判断userInfo集合中有无该用户的_openid
    }).get().then(res=>{
        if (res.data.length !=0) {
          app.collectionOpenid = true
          console.log("云数据库有当前用户信息，直接跳转首页");
          wx.cloud.callFunction({
            name: "getEvevtNews"
          }).then(res4=>{
            this.setData({
              EventNews: res4.result.data
            })
            // console.log("EventNews",this.data.EventNews);
          })
        }

        // 根据userInfo集合中是否有_openid来判断用户是否首次登录和app.tourist判断是否游客
        if(!app.collectionOpenid){
          if (!app.tourist) {
            console.log("用户未授权登录，退回授权页面");
            wx.redirectTo({
            url: '../authorization/authorization',
            })
          }
        }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 显示加载中
    wx.showLoading({
      title: '加载中',
    })
    setTimeout(function () {
      wx.hideLoading()
    }, 1000)

    if (app.tourist) {
      console.log("用户以游客身份进入小程序！");
    }
    // this.getListData()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 调用查询用户openid的函数
    this.getUserOpenId()
    // 调用判断用户openid和集合里的openid是否相符
    this.judgeOpenid()
  },

  getNewid(e){
    app.currentEventNewid = e.currentTarget.dataset.id
    // getEventNewByid
    wx.redirectTo({
      url: '../showBox/eventnew/eventnew',
    })
  },

  subscribe: function () {
    wx.showModal({
      title: '提示',
      content: '已订阅（系统会在比赛开始前通知您。）'
    })
  },

  GoHome11(){
    wx.redirectTo({
      url: '../../pages/showBox/showClthes11h/showClthes',
    })
  },

  GoHome13(){
    wx.redirectTo({
      url: '../../pages/showBox/showClthes12h/showClthes',
    })
  },

  GoHome12(){
    wx.redirectTo({
      url: '../../pages/showBox/showClthes13h/showClthes',
    })
  },
  
  // 官方公告详情页跳转
  ToDetail(){
    wx.redirectTo({
      url: '../../pages/showBox/officeDetail01/officeDetail01',
    })
  },
  
  toShow(){
    wx.switchTab({
      url: '../show/show',
    })
  }
})